<?php

namespace app\api\controller;

use data\service\TimeLineService;
use think\Request;

class TimeLine extends BaseApi
{

    private $service;

    protected function initialize()
    {
        parent::initialize();
        $this->service = new TimeLineService();
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $page = $request->get('page',1);
        $size = $request->get('size',8);
        $extra = [];
        list($count,$data) = array_values($this->service->getByPage($page,$size,'*',$extra));
        return $this->returnInfo(1,'',[
            'count'=>$count,
            'data'=>$data,
            'page'=>$page,
            'size'=>$size
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $post = $request->only(['content'], 'post');
        $validate = new \app\api\validate\Notice;
        if(!$validate->check($post)){
            return $this->returnInfo(1,$validate->getError());
        }

        if($this->service->add($post)){
            return $this->returnInfo(0,"添加成功!",['url'=>url('/admin/timeLine')]);
        }else{
            return $this->returnInfo(1,$this->service->getError());
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $post = $request->only(['content'], 'put');

        $validate = new \app\api\validate\Notice;
        if(!$validate->check($post)){
            return $this->returnInfo(1,$validate->getError());
        }

        if($this->service->edit($id,$post)){
            return $this->returnInfo(0,"编辑成功!",['url'=>url('/admin/timeLine')]);
        }else{
            return $this->returnInfo(1,$this->service->getError());
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if($this->service->delete($id)){
            return $this->returnInfo(0,"删除成功!",['url'=>url('/admin/label')]);
        }else{
            return $this->returnInfo(1,$this->service->getError());
        }
    }
}
